package com.johnson.news.wxapi;

import android.content.Intent;
import android.os.Bundle;

import com.johnson.news.R;
import com.johnson.news.event_msg.ShareMsg;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.youmbe.library.BaseActivity;
import com.youmbe.library.util.Constants;
import com.youmbe.library.util.MyToast;
import com.youmbe.library.view.TopBarView;

import org.greenrobot.eventbus.EventBus;


public class WXEntryActivity extends BaseActivity implements IWXAPIEventHandler {
    private TopBarView mTopbarView;
    private IWXAPI api;
    private static final int RETURN_MSG_TYPE_LOGIN = 1;
    private static final int RETURN_MSG_TYPE_SHARE = 2;
    public static boolean cancelShare = false; // 用户是否取消了分享

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wx_callback);
        mTopbarView = findViewById(R.id.topbar_wx_callback);
        mTopbarView.setTitle("微信");
        //
        api = WXAPIFactory.createWXAPI(this, Constants.WEIXIN_APP_ID, true);
        api.handleIntent(getIntent(), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
        finish();
    }

    @Override
    public void onReq(BaseReq req) {
        finish();
    }

    @Override
    public void onResp(BaseResp resp) {
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_AUTH_DENIED:  //用户拒绝授权
                MyToast.showToast("登录失败", WXEntryActivity.this);
            case BaseResp.ErrCode.ERR_USER_CANCEL:  //取消
                cancelShare = true;
                MyToast.showToast("用户取消", WXEntryActivity.this);
                finish();
                break;
            case BaseResp.ErrCode.ERR_OK: //成功，用户同意
                switch (resp.getType()) {
                    case RETURN_MSG_TYPE_LOGIN:
                        String code = ((SendAuth.Resp) resp).code;
//                        getWeChatAccessToken(code);
                        Bundle b = new Bundle();
                        b.putString("code", code);
//                        new Coco3gBroadcastUtils(WXEntryActivity.this).sendBroadcast(Coco3gBroadcastUtils.WEIXIN_LOGIN_SUCCESS, b);//登录功能
                        finish();
                        break;
                    case RETURN_MSG_TYPE_SHARE:
                        MyToast.showToast("微信分享成功", WXEntryActivity.this);
//                        new Coco3gBroadcastUtils(WXEntryActivity.this).sendBroadcast(Coco3gBroadcastUtils.SHARE_SUCCESS, null);
                        EventBus.getDefault().post(new ShareMsg(ShareMsg.share_type_wx));
                        finish();
                        break;
                }
                break;
        }

    }

    /**
     * 获取微信AccessToken
     *
     * @param code
     */
//    public void getWeChatAccessToken(final String code) {
//        HashMap<String, String> params = new HashMap<>();
//        params.put("appid", Constants.WEIXIN_APP_ID);
//        params.put("secret", Constants.WEIXIN_APP_SECRET);
//        params.put("code", code);
//        params.put("grant_type", "authorization_code");
//        new WeChatBasePresenter(this).progressShow(true, "正在登录微信").addRequestParams(params).getWeChatAccessToken(new BaseStringListener() {
//            @Override
//            public void onSuccess(String response) {
//                if (TextUtils.isEmpty(response)) {
//                    MyToast.showToast("数据返回错误，请检查网络重试...", WXEntryActivity.this);
//                    return;
//                }
//                final Type type = new TypeToken<Map<String, Object>>() {
//                }.getType();
//                Gson gson = new Gson();
//                final Map<String, Object> access_tokenMap = gson.fromJson(response, type);
//                if (access_tokenMap != null) {
////                    getUserInfo(access_tokenMap.get("access_token") + "", access_tokenMap.get("openid") + "");
//                    String openid = (String) access_tokenMap.get("openid");
//                    String access_token = (String) access_tokenMap.get("access_token");
//                    String unionid = (String) access_tokenMap.get("unionid");
//                    Log.e("access_token-openid", access_token + "--" + openid + "--" + unionid);
//                    // 微信登录成功，发送广播
//                    Bundle b = new Bundle();
//                    b.putString("access_token", access_token);
//                    b.putString("openid", openid);
//                    b.putString("unionid", unionid);
//                    b.putString("code", code);
//                    new Coco3gBroadcastUtils(WXEntryActivity.this).sendBroadcast(Coco3gBroadcastUtils.WEIXIN_LOGIN_SUCCESS, b);
//                    finish();
//                } else {
//                    // 微信登录失败，发送广播
//                    new Coco3gBroadcastUtils(WXEntryActivity.this).sendBroadcast(Coco3gBroadcastUtils.WEIXIN_LOGIN_FAILURE, null);
////                Intent intent = new Intent("wx_login");
////                sendBroadcast(intent);
////                    mProgress.dismiss();
//                    finish();
//                }
//
//            }
//
//            @Override
//            public void onFailure(String data) {
////                mProgress.dismiss();
//                MyToast.showToast("数据返回错误，请检查网络重试...", WXEntryActivity.this);
//            }
//
//            @Override
//            public void onError(String errormsg) {
////                mProgress.dismiss();
//                MyToast.showToast("数据返回错误，请检查网络重试...", WXEntryActivity.this);
//            }
//        });
//    }

}